package com.xm.xmapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zbar.ZBarView;

public class TestScanActivity extends AppCompatActivity implements
		QRCodeView.Delegate {
	private static final String TAG = TestScanActivity.class.getSimpleName();
	private static final int REQUEST_CODE_CHOOSE_QRCODE_FROM_GALLERY = 666;
	private QRCodeView mQRCodeView;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test_scan);
		mQRCodeView = (ZBarView) findViewById(R.id.zbarview);
		mQRCodeView.setDelegate(this);
	}

	@Override
	protected void onStart() {
		super.onStart();
		mQRCodeView.startCamera();
		mQRCodeView.showScanRect();
		mQRCodeView.startSpot();
	}

	@Override
	protected void onStop() {
		mQRCodeView.stopCamera();
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		mQRCodeView.onDestroy();
		super.onDestroy();
	}

	private void vibrate() {
		Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
		vibrator.vibrate(200);
	}

	@Override
	public void onScanQRCodeSuccess(String result) {
		Log.i(TAG, "result:" + result);
		Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
		vibrate();
		mQRCodeView.startSpot();
	}

	@Override
	public void onScanQRCodeOpenCameraError() {	
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.start_spot:
			mQRCodeView.startSpot();
			break;
		case R.id.stop_spot:
			mQRCodeView.stopSpot();
			break;
		case R.id.start_spot_showrect:
			mQRCodeView.startSpotAndShowRect();
			break;
		case R.id.stop_spot_hiddenrect:
			mQRCodeView.stopSpotAndHiddenRect();
			break;
		case R.id.show_rect:
			mQRCodeView.showScanRect();
			break;
		case R.id.hidden_rect:
			mQRCodeView.hiddenScanRect();
			break;
		case R.id.start_preview:
			mQRCodeView.startCamera();
			break;
		case R.id.stop_preview:
			mQRCodeView.stopCamera();
			break;
		case R.id.open_flashlight:
			mQRCodeView.openFlashlight();
			break;
		case R.id.close_flashlight:
			mQRCodeView.closeFlashlight();
			break;
		case R.id.scan_barcode:
			mQRCodeView.changeToScanBarcodeStyle();
			break;
		case R.id.scan_qrcode:
			mQRCodeView.changeToScanQRCodeStyle();
			break;
		case R.id.choose_qrcde_from_gallery:

			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK
				&& requestCode == REQUEST_CODE_CHOOSE_QRCODE_FROM_GALLERY) {
			
		}
	}


	public static void rgba2Yuv420(byte[] src, byte[] dst, int width, int height) {
		for (int y = 0; y < height; y++) {
			int dstOffset = y * width;
			int srcOffset = y * width * 4;
			for (int x = 0; x < width && dstOffset < dst.length
					&& srcOffset < src.length; x++) {
				dst[dstOffset] = src[srcOffset];
				dstOffset += 1;
				srcOffset += 4;
			}
		}
		/* Cb and Cr */
		for (int y = 0; y < height / 2; y++) {
			int dstUOffset = y * width + width * height;
			int srcUOffset = y * width * 8 + 1;

			int dstVOffset = y * width + width * height + 1;
			int srcVOffset = y * width * 8 + 2;
			for (int x = 0; x < width / 2 && dstUOffset < dst.length
					&& srcUOffset < src.length && dstVOffset < dst.length
					&& srcVOffset < src.length; x++) {
				dst[dstUOffset] = src[srcUOffset];
				dst[dstVOffset] = src[srcVOffset];

				dstUOffset += 2;
				dstVOffset += 2;

				srcUOffset += 8;
				srcVOffset += 8;
			}
		}
	}
}